package com.lufan.util;

public class PathUtil {
    public static String sanitizePath(String originalPath) {
        String sanitized = originalPath
                .replaceAll("[<>:\"/\\\\|?*]", "_")
                .replaceAll("\\s+", " ")
                .trim();
        if (sanitized.matches("^(CON|PRN|AUX|NUL|COM[1-9]|LPT[1-9])(\\.|$).*")) {
            sanitized = "FILE_" + sanitized;
        }
        int maxLength = 260 - 10;
        if (sanitized.length() > maxLength) {
            String extension = "";
            int dotIndex = sanitized.lastIndexOf('.');
            if (dotIndex > 0) {
                extension = sanitized.substring(dotIndex);
                sanitized = sanitized.substring(0, dotIndex);
            }
            sanitized = sanitized.substring(0, maxLength - extension.length()) + extension;
        }
        return sanitized;
    }
}
